package com.wt.admin.config.i18n;

import com.aizuda.easy.security.code.BasicCode;
import com.aizuda.easy.security.exp.IErrorCode;
import com.wt.admin.config.SpringContextHolder;
import lombok.experimental.UtilityClass;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

@UtilityClass
public class MessageHelperConfig {

    public String getMessage(String code, Object... objects) {
        MessageSource messageSource = SpringContextHolder.getBean("messageSource");
        // LocaleContextHolder.getLocale()获取客户端浏览器的语言环境，就是请求头中的Accept-Language的值
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code, objects, locale);

    }

    public String getMessage(Integer code, Object... objects) {
        MessageSource messageSource = SpringContextHolder.getBean("messageSource");
        // LocaleContextHolder.getLocale()获取客户端浏览器的语言环境，就是请求头中的Accept-Language的值
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code.toString(), objects, locale);

    }
    /**
     * 通过code 和参数获取错误信息
     * @param code
     * @return
     */
    public String getMessage(IErrorCode code, Object... objects) {
        MessageSource messageSource = SpringContextHolder.getBean("messageSource");
        // LocaleContextHolder.getLocale()获取客户端浏览器的语言环境，就是请求头中的Accept-Language的值
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code.getCode().toString(), objects, locale);

    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        Locale chinaLocale = Locale.CHINA;
        Locale usLocale = Locale.US;
        System.out.println(chinaLocale);
        System.out.println(usLocale);
    }

}